import detail from '../../../libs/data/recdata.js';
import amounts from '../../../libs/data/buycardata.js';
var WxParse = require('../../../libs/wxParse/wxParse.js');
var api = require('../../../config/api.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {


    images: [],


    detail,
    modNum: '',
    num: 1,
    current: 1,
    hide: true,
    serHide: true,
    amounts,
    image: '/images/shops/s12.png',
    model: '',
    curIndex: 0,
    showtj: true,
    carNum: 0,
    lists: [],
    goodsDesc: "",

    id: '',
    primaryPicUrl: '',
    PicUrllist:[],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '商品详情',
    })

    var that = this;
    console.log("options.id" + options.id)

    that.setData({
      id: options.id,
    });
    var ID = that.data.id;

    wx.request({
      url: api.GoodsInfoById + ID,
      data: {},
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        if (res.data.success) {
          that.setData({
            id: res.data.obj.id,
            primaryPicUrl: res.data.obj.primaryPicUrl,
            listPicUrl: res.data.obj.listPicUrl,
            goodsName: res.data.obj.goodsName,
            accountIndex: res.data.obj.categoryId,
            marketPrice: res.data.obj.marketPrice,
            goodsBrief: res.data.obj.goodsBrief,
            goodsDesc: res.data.obj.goodsDesc,
            goodsSn: res.data.obj.goodsSn,
            keywords: res.data.obj.keywords,
            goodsUnit: res.data.obj.goodsUnit,
            categoryId: res.data.obj.categoryId,
            categoryName: res.data.obj.categoryName,
            marketPrice: res.data.obj.marketPrice,
            retailPrice: res.data.obj.retailPrice,
            goodsNumber: res.data.obj.goodsNumber,
            isHot: res.data.obj.isHot,
          });

          that.setData({
            model: '已选择：' + that.data.num + that.data.goodsUnit,
          })

          var goodsD = that.data.goodsDesc;
          /**
           * WxParse.wxParse(bindName , type, data, target,imagePadding)
           * 1.bindName绑定的数据名(必填)
           * 2.type可以为html或者md(必填)
           * 3.data为传入的具体数据(必填)
           * 4.target为Page对象,一般为this(必填)
           * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
           */
          WxParse.wxParse('goodsD', 'html', goodsD, that, 5);

          var listurl = that.data.listPicUrl.split(";");
          console.log(listurl);
          var img = that.data.images;

          for (var i = 0; i < listurl.length; i++) {
            img[i] = listurl[i];
          }

          that.setData({
            images: img
          })

          wx.hideLoading();
        }
      }
    })

  },

  // 存储购物车数量数据函数
  getCarNum: function (e) {
    var that = this
    let num = 0;
    let lis = wx.getStorageSync("lists");
    for (var i = 0; i < lis.length; i++) {
      if (lis[i].num) {
        num ++
      }
    }
    wx.setStorage({
      key: 'carNum',
      data: num
    })
    that.onShow();
    console.log("更新购物车")
  },



  goBuy: function (e) {
    let num = this.data.num;
    let hide = true;

    if (this.data.hide) {
      hide = false,
        this.setData({
          hide: false
        })
    }
    let temp = {
      id: this.data.id,
      title: this.data.goodsName,
      price: this.data.marketPrice,
      model: this.data.model,
      num: this.data.num,
      image: '',
      primaryPicUrl: this.data.primaryPicUrl,
      selected: false,
      hasCarNum: this.data.carNum,
      isTouchMove: false,

      categoryId: this.data.categoryId,
      categoryName: this.data.categoryName,
      marketPrice: this.data.marketPrice,
      retailPrice: this.data.retailPrice,
      goodsId: this.data.id,
      goodsName: this.data.goodsName,
      goodsNo: this.data.goodsSn,
      goodsNum: 0,
      goodsSales: 0,
    }
    if (this.data.model) {
      var lists = [temp]
      var retailPrice = temp.retailPrice
      var totalPrice = (retailPrice * num).toFixed(2)
      var totalNum = temp.num
      var ordlists = {
        lists,
        totalPrice,
        totalNum
      }
      wx.setStorageSync("ordlists", ordlists)
    }
    console.log(this.data.lists)
    if (hide) {
      if (this.data.model) {
        wx.navigateTo({
          url: '/pages/techgoodpay/techgoodpay',
        })
      } else {
        wx.showToast({
          title: '购买商品数量不能为0',
          icon: 'none'
        })
      }
    }

  },

  goCar(e) {
    wx.navigateTo({
      url: "/pages/techgoodcar/techgoodcar",
      success: (res) => {
        wx.setStorage({
          key: 'carNum',
          data: this.data.carNum
        })
      }
    })
  },

  addCar: function (e) {
    var that = this
    let num = that.data.num;
    let carNum = that.data.carNum;
    let hide = true;

    if (that.data.hide) {
      hide = false,
        that.setData({
          hide: false
        })

    } else {

      let temp = {
        id: that.data.id,
        title: that.data.goodsName,
        price: that.data.marketPrice,
        model: that.data.model,
        num: that.data.num,
        image: '',
        primaryPicUrl: that.data.primaryPicUrl,
        selected: false,
        hasCarNum: that.data.carNum,
        isTouchMove: false,
        createUserDeptId: that.data.createUserDeptId,
        categoryId: that.data.categoryId,
        categoryName: that.data.categoryName,
        marketPrice: that.data.marketPrice,
        retailPrice: that.data.retailPrice,
        goodsId: that.data.id,
        goodsName: that.data.goodsName,
        goodsNo: that.data.goodsSn,
        goodsNumber: that.data.goodsNumber,
        goodsSales: that.data.goodsSales,
      }
      if (that.data.model) {
        var lis = that.data.lists
        var k = 0;
        if (lis.length == 0) {
          var x = []
          x = x.concat(temp)
          that.setData({
            lists: x
          })
        } else {
          for (var i = 0; i < lis.length; i++) {
            if (lis[i].id == temp.id) {
              lis[i].model = temp.model
              lis[i].num += temp.num
              k++
            }
          }
          if (k !== 0) {
            that.setData({
              lists: lis
            })
          } else {
            var l = []
            l = l.concat(temp)
            l = l.concat(lis)
            that.setData({
              lists: l
            })
          }

        }

        wx.setStorageSync('lists', that.data.lists)
        wx.showToast({
          title: '加入购物车成功',
          icon: 'none'
        })
        that.getCarNum()
        that.setData({
          carNum: wx.getStorageSync("carNum")
        })



      } else {
        wx.showToast({
          title: '购买商品数量不能为0',
          icon: 'none'
        })
      }

      that.onShow()
    }

  },

  switchCategory(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index;
    if (index == 0) {
      this.setData({
        showtj: true
      })
    } else {
      this.setData({
        showtj: false
      })
    }
    this.setData({
      curIndex: e.currentTarget.dataset.index ? e.currentTarget.dataset.index : 0,
    })
  },

  bindAmountChange(e) {
    // console.log(e)
    let amounts = this.data.amounts;
    let strVal = e.detail.value;
    let image;
    for (let amount of amounts) {
      amount.checked = amount.value == strVal;
      // console.log(amount)
      if (amount.checked == true) {
        image = amount.image;
      }
    }
    this.setData({
      amounts,
      model: '已选择：' + strVal,
      image
    })
  },

  changeSwiper(e) {
    var current = e.detail.current + 1;
    this.setData({
      current
    })
  },

  actionSheet(e) {
    this.setData({
      hide: false,
    })
  },

  upactionSheet(e) {
    if (this.data.model) {
      this.data.modNum = '*' + this.data.num;
    }
    this.setData({
      hide: true,
      modNum: this.data.modNum,
      model: this.data.model
    })
  },

  actionSer(e) {
    this.setData({
      serHide: false
    })
  },

  upactionSer(e) {
    this.setData({
      serHide: true
    })
  },

  dropNum(e) {
    var that = this;
    let num = that.data.num;
    let model = that.data.model;
    let modNum = that.data.modNum;
    num--;
    if (num <= 1) {
      wx.showToast({
        title: '数量至少为1',
        icon: 'none'
      })
      num = 1;
    }
    if (num >= 1) {
      that.setData({
        model: '已选择：' + num + that.data.goodsUnit,
        num,
      })
    }
  },

  addNum(e) {
    var that = this;
    let num = that.data.num;
    let modNum = that.data.modNum;
    let model = that.data.model;
    if (num < that.data.goodsNumber) {
      num++;
      that.setData({
        model: '已选择：' + num + that.data.goodsUnit,
        num,
      })
    } else {
      wx.showToast({
        title: '已达到最大剩余库存量',
        icon: 'none'
      })
    }
  },

  numchange: function (e) {
    var that = this;
    var x = JSON.parse(e.detail.value)
    if (x > that.data.goodsNumber) {
      wx.showToast({
        title: '购买数量不能大于库存数量',
        icon: 'none'
      })
      that.setData({
        num: that.data.goodsNumber,
      })
    } else if (x <= 0) {
      wx.showToast({
        title: '请至少购买一件商品',
        icon: 'none'
      })
      that.setData({
        num: 1,
      })
    } else {
      that.setData({
        num: x,
      })
    }
    that.setData({
      model: '已选择：' + that.data.num + that.data.goodsUnit,
    })

  },






  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getStorage({
      key: 'carNum',
      success: (res) => {
        this.setData({
          carNum: res.data
        })
        app.globalData.carNum = this.data.carNum;
      }
    })
    wx.getStorage({
      key: 'lists',
      success: (res) => {
        this.setData({
          lists: res.data

        })
      },

    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})